package com.fishingwithme.android.ui.screens.map.handlers

import com.fishingwithme.android.R

/**
 * 清理处理器 - 用于清理地图数据和元素
 */
class CleanUpHandler : BaseHandler() {
    override val key: String = "cleanup"
    override val icon: Int = R.mipmap.clean  // 使用"delete"图标表示清理操作

    override fun doAction() {
        super.doAction()
        // 清理操作在reset方法中实现，确保在处理器停用时执行清理
        this.reset()
    }

    override fun reset() {
        super.reset()
        try {
            // Clean up all map elements through the context
            mapContext?.clearAllElements()

            // Clean up all data except session ID (if needed for navigation)
            mapContext?.data?.let { data ->
                val sessionId = data["sessionId"]
                data.clear()
                // Preserve session ID if it exists
                if (sessionId != null) {
                    data["sessionId"] = sessionId
                }
            }

            android.util.Log.d("CleanUpHandler", "已清理所有地图元素和数据")
        } catch (e: Exception) {
            android.util.Log.w("CleanUpHandler", "Error during cleanup", e)
        }
    }
}